package com.hh85.sqlite;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;
import java.util.HashMap;

public class KeepActivity extends AppCompatActivity {

    private DatabaseUtils databaseUtils;
    private Spinner spinner;
    private EditText priceText;
    private TextView timeText;
    private ImageView selectDate;
    private EditText remarkText;
    private String type ="收入";
    private DatePickerDialog dialog;
    private Button addBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_keep);
        Calendar c = Calendar.getInstance();
       int mYear = c.get(Calendar.YEAR);
       int mMonth = c.get(Calendar.MONTH);
       int mDay = c.get(Calendar.DAY_OF_MONTH);
        dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year,
                                  int month, int day) {
                System.out.println(year +"月："+ month +"day:"+ day);
                timeText.setText(year +"-"+ (month+1) +"-"+ day);
            }
        },mYear,mMonth,mDay);
        databaseUtils = new DatabaseUtils(getBaseContext(),"book");
        databaseUtils.open();
        spinner  = (Spinner)findViewById(R.id.id_type);
        final String[] mItems = {"收入", "支出"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mItems);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                type = mItems[i];
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        priceText = (EditText)findViewById(R.id.id_price);
        timeText = (TextView)findViewById(R.id.id_time);


        selectDate = (ImageView)findViewById(R.id.id_select_date);
        selectDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.show();
            }
        });
        remarkText = (EditText)findViewById(R.id.id_remark);

        addBtn = (Button)findViewById(R.id.id_add);
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                HashMap<String,String> param = new HashMap<>();
                param.put("acid",System.currentTimeMillis()+"");
                param.put("type",type);
                param.put("time",timeText.getText().toString());
                param.put("price",priceText.getText().toString());
                param.put("remark",remarkText.getText().toString());
                long num =  databaseUtils.insert("account",param);
                if(num > 0){
                    Log.e("TAG","写入成功"+num);

                    Intent intent = new Intent();

                    setResult(RESULT_OK,intent);
                    KeepActivity.this.finish();

                }

            }
        });

    }


}
